package operator.combine;

import java.util.concurrent.TimeUnit;

import io.reactivex.Observable;
import io.reactivex.functions.BiFunction;
import io.reactivex.functions.Function;
import util.RxJavaHelper;
import util.ThreadHelper;

class ZipOperator {

    public static void main(String[] args) {
        zip();
    }

    //会将多个被观察者合并，根据各个被观察者发送事件的顺序一个个结合起来
    //最终发送的事件数量会与源 Observable 中最少事件的数量一样
    //严格按照顺序
    static void zip() {
        Observable.zip(
                Observable.just(1, 2, 3, 4, 5).delay(3000, TimeUnit.MILLISECONDS)
                        .map(new Function<Integer, String>() {
                            @Override
                            public String apply(Integer integer) throws Exception {
                                return "A " + integer.toString();
                            }
                        })
                , Observable.just(6, 7, 8, 9)
                        .map(new Function<Integer, String>() {
                            @Override
                            public String apply(Integer integer) throws Exception {
                                return "B " + integer.toString();
                            }
                        })
                , new BiFunction<String, String, String>() {  //组合方式
                    @Override
                    public String apply(String string1, String string2) throws Exception {
                        return string1 + " and " + string2;
                    }
                }
        ).subscribe(RxJavaHelper.getConsumer());

        ThreadHelper.sleep();
    }


}
